<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>资源管理</title>
	<#include '/commons/include/head.ftl'>
</head>

<body>

	<div id="tg">
		<div id="toolbars" style="display:none;">
			<form id="searchForm">
				<div class="datagrid-toolbars">
					<div class="toolbar-row">
						<div class="group">
							<a onclick="form_dialog('add');" href="javascript:;" class="bto bto-green"><i class="fa fa-plus"></i><span>新增</span></a>
							<a onclick="form_dialog('update');" href="javascript:;" class="bto bto-blue"><i class="fa fa-pencil"></i><span>编辑</span></a>
							<a onclick="del();" href="javascript:;" class="bto bto-red"><i class="fa fa-trash"></i><span>删除</span></a>
							<a onclick="reload();" href="javascript:;" class="bto bto-purple1"><i class="fa fa-refresh"></i><span>刷新</span></a>
						</div>
						<div class="group">
							<input id="menu" class="easyui-validatebox" type="text" style="width: 150px; height: 25px;" />
						</div>
					</div>
				</div>
			</form>
		</div>
	</div>
	
	
	<script>
	var $tg, $menu ;
	$(function(){
		loadResource();
		
		$menu = $("#menu").combobox({
			url: "${ctx}/manager/system/menu/getAllList",
			valueField: "id", panelHeight: 180,
			onSelect: function(record) {
				$tg.treegrid({url: "${ctx}/manager/system/resource/treegridByMenu/"+record.id}) ;
			},
			onLoadSuccess: function() {
				var data= $menu.combobox("getData") ;
				if(data.length > 0) {
					$menu.combobox("setValue", data[0].id);
					$tg.treegrid({url: "${ctx}/manager/system/resource/treegridByMenu/"+data[0].id}) ;
				} else {
					$(".bto").attr("onclick", "").click(function(){
						alertify.warning("目前没有菜单，请先创建菜单！");
					}); ;
				}
			}
		});
	});
	
	function loadResource() {
		$tg = $("#tg").treegrid({
			title: "资源管理",
            idField: "id", treeField: "text", fit: true, 
            border: false, striped:true, singleSelect: true,rownumbers: true, 
            toolbar: "#toolbars", sortName: 'sorting', sortOrder: 'asc', queryParams: {},
            frozenColumns: [[
                { field: "id", title: "ID", width: 50, sortable: true, hidden: true } 
            ]],
            columns: [[
                { field: "text", title: "名称", width: 180 },
                { field: "identity", title: "权限标识", width: 120 },
                { field: "operation", title: "操作", width: 200, formatter:function(value,row){
                	var str = [] ;
                	if(undefined != row.permits && row.permits.length > 0) {
                		$.each(row.permits, function(i, p){
		                	str.push("&nbsp;"+p.name);
                		});
                	}
                	return str.join(",") ;	
                }}, 
                { field: "href", title: "URL", width: 350 },
                { field: "status", title: "是否禁用", width: 60, sortable: true, align: "center", formatter:function(value,row){
                	var t = "<span style='color: red;'>禁用</span>", f = "<span style='color: green;'>正常</span>" ;
                	return (value?t:f) ;	
                }},
                { field: "sorting", title: "排序", width: 50, align: "center", sortable: true },
                { field: "createTime", title: "创建时间", width: 150, sortable: true, align: "center", formatter:function(value,row){
                	return (undefined != value?$.date.format(value, "yyyy-MM-dd HH:mm:ss"):"") ;	
                }},
                { field: "updateTime", title: "修改时间", width: 150, sortable: true, align: "center", formatter:function(value,row){
                	return (undefined != value?$.date.format(value, "yyyy-MM-dd HH:mm:ss"):"") ;	
                }}
            ]],
            onLoadSuccess: function(){
            	$.fn.treegrid.extensions.onLoadSuccess.apply(this, arguments);
            	$tg.treegrid('unselectAll');$tg.treegrid('clearSelections');
            },
            enableHeaderClickMenu: true, enableHeaderContextMenu: true, enableRowContextMenu: false
        });
	}
	
	
	
	function reload(){$tg.treegrid("reload") ;}
	function getIds() {
		var rows = $tg.datagrid('getChecked');
		var ids = [];
		if (rows.length > 0) {
			for ( var i = 0; i < rows.length; i++) {
				if(undefined != rows[i].id && ""!=rows[i].id) {
					ids.push(rows[i].id);
				}
			}
		}
		return ids ;
	}	
	
	function form_dialog(formType) {
		var menuid = $menu.combobox("getValue") ;
		var form_url = "${ctx}/manager/system/resource/form?menuId="+menuid;
		if(formType === "update") {
			var ids = getIds() ;
			if(ids.length == 0) {alertify.warning("请选择一条记录！");return;}
			if(ids.length > 1) {alertify.warning("只能编辑一条记录！");return;}
			form_url = "${ctx}/manager/system/resource/form?menuId="+menuid+"&id="+ids[0];
		}
		
		var $d = $.easyui.showDialog({
			title: '<div class="icon-div"><i class="fa fa-link fa-120 pt-2 fa-c-blue"></i></div> 表单', 
			href: form_url, 
			iniframe: false, topMost: true, iconCls : '',
			width: 610, height: 574, maximizable: true,
            enableApplyButton: false, enableCloseButton: false, enableSaveButton: false,
            buttons : [ 
              { text : '<i class="fa fa-save fa-120 mt-6 fa-c-green"></i> 应用', handler : function() { $.easyui.parent.submitForm($d, $tg, true) ; } },
              { text : '<i class="fa fa-save fa-120 mt-6 fa-c-blue"></i> 保存', handler : function() { $.easyui.parent.submitForm($d, $tg) ; } },
              { text : '<i class="fa fa-remove fa-120 mt-6 fa-c-red"></i> 关闭', handler : function() { $d.dialog('destroy'); } } 
           	]
        });
	}
	
	function del() {
		var node = $tg.treegrid("getSelected");
		if(undefined == node || null == node) {alertify.warning("请选择一条记录！");return;}
		$.messager.confirm("您确定要进行该操作？", function (c) { 
			if(c) {
				$.easyui.loading({ msg: "数据删除中，请稍等..."});
				deletedAjax("${ctx}/manager/system/resource/delete", {"id": node.id}) ;
			}
		});
	}
	function deletedAjax(url, params) {
		$.post(url, params, function(result) {
			if (result.status) {
				$tg.treegrid("unselectAll");$tg.treegrid("reload") ;
				alertify.success(result.message);
				$.easyui.loaded();
			} else {
				alertify.warning(result.message);
				$.easyui.loaded();
			}
		}, 'json').error(function() { $.easyui.loaded(); });
	}
	</script>
</body>
</html>

